package com.jeesite.modules.kj.api.controller.activity;

import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.entity.KjLike;
import com.jeesite.modules.kj.service.KjLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "点赞api", tags = {"点赞API"})
@Slf4j
@Controller
@RequestMapping(value = "/api/like")
public class LikeApiController {
    @Autowired
    KjLikeService kjLikeService;


    @ApiOperation(value = "获取点赞总数", notes = "获取点赞总数")
    @GetMapping(value = "count")
    public ResponseBean<Long> count(String relationId, String relationType) {
        KjLike where = new KjLike();
        where.setLikeType(relationType);
        where.setRelationId(relationId);
        Long count = kjLikeService.findCount(where);
        return new ResponseBean<>(count);
    }



    @ApiOperation(value = "点赞", notes = "点赞")
    @PostMapping(value = "add")
    @UserLoginToken
    public ResponseBean<String> add(@ApiIgnore @CurrentUser String memberId, String relationId, String relationType) {
        KjLike where = new KjLike();
        where.setLikeType(relationType);
        where.setRelationId(relationId);
        where.setMemberId(memberId);
        long count = kjLikeService.findCount(where);
        if(count == 0){
            kjLikeService.save(where);
            return new ResponseBean<>("点赞成功");
        }else{
            return new ResponseBean<>("已点赞");
        }

    }


    @ApiOperation(value = "取消点赞", notes = "取消点赞")
    @PostMapping(value = "cancel")
    @UserLoginToken
    public ResponseBean<String> cancel(@ApiIgnore @CurrentUser String memberId, String relationId, String relationType) {
        KjLike where = new KjLike();
        where.setLikeType(relationType);
        where.setRelationId(relationId);
        where.setMemberId(memberId);
        List<KjLike> likes = kjLikeService.findList(where);
        if(likes.size() > 0){
            for (KjLike like: likes
                 ) {
                kjLikeService.delete(like);
            }
            return new ResponseBean<>("取消点赞成功");
        }else{
            return new ResponseBean<>("未点赞");
        }
    }
}
